/*
 * Copyright (c) 2009, Jan Pastor. All Rights Reserved.
 */

package sk.jp.jitag;

import java.util.Arrays;
import java.util.List;
import javax.swing.UIManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import sk.jp.commons.exception.ApplicationException;
import sk.jp.commons.ui.ExceptionDialog;
import sk.jp.commons.utils.CollectionUtils;
import sk.jp.commons.utils.LogMsg;
import sk.jp.jitag.ui.MainWindow;
import sk.jp.jitag.util.Property;

/**
 * Application entry-point.
 */
@Component(value = Main.beanId)
public class Main {

    public static final String beanId = "jitagMain";

    private static final Logger log = LoggerFactory.getLogger(Main.class);

    @Autowired
    private ConfigManager configManager;

    @Autowired
    private ApplicationLogic applicationLogic;

    @Autowired
    private UiManager uiManager;

    @Autowired
    private MainWindow mainWindow;

    //----------------------------------------------------------------------

    private void start(List<String> args){
        log.debug(LogMsg.inp("args", CollectionUtils.listToString(args, ", ")));

        configManager.loadConfig(Property.confPlace.value(), Property.confPath.value());

        applicationLogic.initEnums();

        try {
            uiManager.setupLookAndFeel(configManager.getConfig().getCurrentProfile());
        } catch (ApplicationException ae) {
            throw ae;
        } catch (Exception e) {
            throw new ApplicationException("Can't initialize user interface",
                                           null, null, e);
        }
        mainWindow.open();
        log.trace(LogMsg.out());
    }

    //----------------------------------------------------------------------

    public static void main(String[] args){
        try {
            log.debug("Starting application...");
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
            log.info("Application context initialized");
            Main main = (Main) ctx.getBean(beanId);
            main.start(Arrays.asList(args));
            log.debug("Application started");
        } catch (Throwable e) {
            try {
                log.error("Unexpected exception, see logs for details. Application go down...", e);
                //Try to show exception in GUI dialog
                try {
                    UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    ExceptionDialog.show(null, e);
                }catch(Throwable ignored){
                }
                //noinspection CallToSystemExit
                System.exit(-1);
            }catch(Exception ignored){
                e.printStackTrace(System.err);
            }
        }
    }

}
